package com.junit;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hcfly.utils.ESClient;
import org.elasticsearch.action.search.*;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.io.IOException;

public class ScrollDemo {
    private static final ObjectMapper mapper = new ObjectMapper();
    private static final String index = "fruit";
    private static final String type = "species";
    private static final RestHighLevelClient client = ESClient.getClient();
    @Test
    public void scrollQuery() throws IOException {

        /**
         * 深度分页，scroll+size，比from+size更舒服
         *
         */

        // 1,创建SearchRequest
        SearchRequest request = new SearchRequest(index);
        request.types(type);

        // 2,指定scroll信息, 保存在内存中的上下文时长
        request.scroll(TimeValue.timeValueMinutes(1L));

        //3,指定查询条件
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.size(4);
        builder.sort("size", SortOrder.DESC);
        builder.query(QueryBuilders.matchAllQuery());

        request.source(builder);

        // 4，进行查询，并得到scroll_id和source
        SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
        String scrollId = resp.getScrollId();
        System.out.println("---------------------首页---------------------");
        for (SearchHit hit : resp.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());
        }

        while (true) {
            // 5,无限循环，并且创建searchScrollRequest对象
            SearchScrollRequest searchScrollRequest = new SearchScrollRequest(scrollId);
            // 6,设置内存上下文生存时间
            searchScrollRequest.scroll(TimeValue.timeValueMinutes(1L));
            // 7, 执行查询返回查询结果
            SearchResponse scrollResp = client.scroll(searchScrollRequest, RequestOptions.DEFAULT);
            // 8, 判断是否出现有结果，并且输出
            SearchHit[] hits = scrollResp.getHits().getHits();
            if(hits != null && hits.length > 0){
                System.out.println("-----------下一页-------------");
                for (SearchHit hit : hits) {
                    System.out.println(hit.getSourceAsMap());
                }
            }else {
                System.out.println("---------------------结束--------------------");
                break;
            }
        }

        // 9，清楚scroll
        ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
        // 10，指定ID
        clearScrollRequest.addScrollId(scrollId);
        //11,删除Scroll
        ClearScrollResponse clearScrollResponse = client.clearScroll(clearScrollRequest, RequestOptions.DEFAULT);
        //12 查看是否删除成功
        System.out.println(clearScrollResponse.isSucceeded());

    }
}
